package com.chinastock.log;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;

/**
 * @author zhangfan.it
 * @date 2021/1/22
 */
@Component
@Order(Filters.REQUEST_LOG)
public class RequestLogFilter implements WebFilter {
    @Value("${request.log.switch:true}")
    private boolean isOn;

    @Value("${request.log.excludes:}")
    private String excludes;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        if (!isOn) {
            return chain.filter(exchange);
        }

        return chain.filter(new ExtractRequestBodyServerWebExchangeDecorator(exchange, excludes, isOn));
    }
}
